package biz.datalk.industrialland.common.log.service.impl;

import biz.datalk.commons.utils.json.JsonUtil;
import biz.datalk.industrialland.common.log.config.LogProperties;
import biz.datalk.industrialland.common.log.def.LogTypeEnum;
import biz.datalk.industrialland.common.log.def.OptEnum;
import biz.datalk.industrialland.common.log.def.OptResultEnum;
import biz.datalk.industrialland.common.log.pojo.bo.LogBO;
import biz.datalk.industrialland.common.log.service.ISysLogService;
import biz.datalk.industrialland.common.log.util.SysLogUtil;
import biz.datalk.industrialland.common.util.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 日志服务实现类
 *
 * 存储到 MySQL
 *
 * @author tarofang@163.com
 * @date 2020-11-24
 */
@Slf4j
public class SysLogServiceDefaultImpl implements ISysLogService {


    private final String logServerIp;
    private final String serverPrivateKey;

    private final LogProperties logProperties;

    public SysLogServiceDefaultImpl(LogProperties logProperties) {
        this.logProperties = logProperties;
        this.logServerIp = logProperties.getLogServerIp();
        this.serverPrivateKey = logProperties.getServerPrivateKey();
    }

    @Override
    public void saveLogBo(LogBO logBO) {
        if (!logProperties.getDefaultImplPrintLog()) {
            return;
        }
        this.savePreview(logBO, Boolean.TRUE);
    }

    @Override
    public void saveLog(Long sysUserId, String username, LogTypeEnum logTypeEnum, OptEnum optEnum, String logContent,//
                        OptResultEnum opResult, String reqResult, String reqBodyPlaintext, long costMillisecond) {
        if (!logProperties.getDefaultImplPrintLog()) {
            return;
        }
        sysUserId = sysUserId == null ? -1L : sysUserId;
        username = StringUtils.isBlank(username) ? "" : username.trim();
        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        LogBO logBO = SysLogUtil.pkgLogBo(request, sysUserId, username, logTypeEnum, optEnum, logContent, opResult, reqResult, reqBodyPlaintext, costMillisecond, this.logServerIp);
        this.savePreview(logBO, Boolean.FALSE);
    }

    @Override
    public void saveLog(Long sysUserId, String username, LogTypeEnum logTypeEnum, OptEnum optEnum, String logContent, //
                        OptResultEnum opResult, String reqResult, String reqBodyPlaintext) {
        this.saveLog(sysUserId, username, logTypeEnum, optEnum, logContent, opResult, reqResult, reqBodyPlaintext, 0L);
    }

    @Override
    public void saveLog(LogTypeEnum logTypeEnum, OptEnum optEnum, String logContent, OptResultEnum opResult, String reqResult, String reqBodyPlaintext) {
        this.saveLog(null, null, logTypeEnum, optEnum, logContent, opResult, reqResult, reqBodyPlaintext, 0L);
    }

    private void savePreview(LogBO logBO, boolean needDecryptReqBodyParam) {
        if (log.isDebugEnabled()) {
            log.debug("SysLogServiceDefaultImpl ============================== into");
            log.debug("SysLogServiceDefaultImpl: LogBO: {}, needDecryptReqBodyParam: {}", logBO, needDecryptReqBodyParam);
        }
        try {

            if (needDecryptReqBodyParam) {
                String decryptReqBody = SysLogUtil.decryptReqBody(logBO.getReqBodyParam(), this.serverPrivateKey);
                logBO.setReqBodyPlaintext(decryptReqBody);
            }

            String jsonStr = JsonUtil.getJsonFromObject(logBO, Boolean.TRUE);
            log.info("log:[{}]", jsonStr);
        } finally {
            if (log.isDebugEnabled()) {
                log.debug("SysLogServiceDefaultImpl ============================== out");
            }
        }
    }


}
